<template>
  <div class="register-container">
    <el-card class="register-card">
      <template #header>
        <div class="flex-row">
          <el-image src="/logo.png"></el-image>
          <span style="font-size: 24px;font-weight: bold;margin-left: 10px">用户注册</span>
        </div>
      </template>

      <base-form
          ref="formRef"
          v-model="registerForm"
          :fields="formFields"
      />

      <div>
        <el-button size="large" style="width: 100%" type="primary" @click="handleRegister">
          <el-icon><Select/></el-icon>
          <span>确定</span>
        </el-button>
      </div>

      <el-button link size="small" style="margin-top: 10px;margin-right: 0" @click="router.push('/login')">
        返回登录
      </el-button>
    </el-card>
  </div>
</template>

<script setup>
import {ref} from 'vue'
import {useRouter} from 'vue-router'
import {Select} from '@element-plus/icons-vue'
import BaseForm from "@/components/common/BaseForm.vue";
import request from "@/utils/request.js";
import {ElMessage} from "element-plus";
import {fields} from "@/model/User.js";

const formFields = fields.map(e => {
  if (e.prop === 'role') {
    e.options = [
      {value: 1, label: '用户', type: 'primary'},
      {value: 2, label: '商家', type: 'info'}
    ]
  }
  return e
})
const router = useRouter()
const formRef = ref(null)
const registerForm = ref({})

const handleRegister = async () => {
  await formRef.value.validate()
  await request.post('/user/register', registerForm.value)
  ElMessage.success('注册成功')
  await router.push('/login')
}
</script>

<style scoped>
.register-container {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: url("/back.png");
}

.register-card {
  width: 400px;
}
</style>